// redirect_demo.c
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>

int main() {
    int fd = open("output.txt", O_WRONLY | O_CREAT | O_TRUNC, 0666);
    if (fd == -1) {
        perror("open");
        return 1;
    }

    dup2(fd, 1); // 将标准输出重定向到文件
    close(fd);

    printf("This is redirected output.\n");

    return 0;
}
